$(function() {
	btnclick();
	ajaxJson(1,10);
});
var isSearch = 0;//默认是不搜索，1是搜索
var currentMsg;
function searchAll(){
	isSearch = 1;
	ajaxJson(1,10);
}
function btnclick(){
	$("#group_btns button").bind('click',function(){
		var remarkId = $(this).data("id");
		tipshow(remarkId,function(_code){
			var _key = $("#tbodys tr.success").first().data("id");//当前数据主键
			if(_code == "del"){
				if(confirm("确定要删除吗")){
					delBtn(_key);
				}
			}else if(_code == "edit"){
				var currentdata = initEditShow(_key);
				if(currentdata){
					$("#cateNameEdit").val(currentdata.cateName);
					$("#cateDescEdit").val(currentdata.cateDesc);
					$("#cateIdEdit").val(currentdata.cateId);
				}
				$("#myModal_"+_code).modal("show");
			}else{//添加
				$("#form_"+_code)[0].reset();
				$("#myModal_"+_code).modal("show");
			}
		});
	});
}
function initEditShow(_key){
	for(var i in currentMsg){
		if(currentMsg[i].cateId == _key){
			return currentMsg[i];
		}
	}
}
//展示tips
function tipshow(code,fn){
	if(code == "edit"|| code == "del"){
		if($("#tbodys tr.success").length > 0){
			$("#editAlert").addClass("hidden");
			fn(code);
		}else{
			$("#editAlert").removeClass("hidden").html("不要忘记选择数据呀");
		}
	}else{//添加
		fn(code);
	}
}
function delBtn(_id){
	jsonAjax("post", "/detector/detectorInfo/del", {"cateId":_id}, "json", function(results){
		if(results.code == 0){//成功
			isSearch = 1;
			ajaxJson(1,10);
		}else{
			alert("删除失败");
		}
	});
}
function submitAddBtn(){
	var cateName = $("#cateNameAdd").val();
	var cateDesc = $("#cateDescAdd").val();
	if(cateName && cateDesc){
		jsonAjax("post", "/detector/detectorInfo/insert", {"cateName":cateName,"cateDesc":cateDesc,"cateLevel":1}, "json", function(results){
			if(results.code == 0){//成功
				$("#myModal_add").modal("hide");
				isSearch = 1;
				ajaxJson(1,10);
			}else{
				alert("添加失败");
			}
		});
	}else{
		alert("将数据填写完整");
	}
}
function submitEditBtn(){
	var _data = $("#form_edit").serializeArray();
	jsonAjax("post", "/detector/detectorInfo/update", _data, "json", function(results){
		if(results.code == 0){//成功
			$("#myModal_edit").modal("hide");
			isSearch = 1;
			ajaxJson(1,10);
		}
	});
}


//根据请求的数据来展示TABLE
function initTables(result){
	if(result.code == 0){
		initPagination(result);
		initTable(result.rs.data);
	}else{
		alert("获取异常");
	}
}
function initPagination(datas){
	var pagenums = datas.rs.page.pageNum;
	var totalpages = datas.rs.page.pages==0?1:datas.rs.page.pages;
	var currentPagea = 1;
	if(isSearch == 0){
		currentPagea = $('#pagination').twbsPagination('getCurrentPage');
	}
	$('#pagination').twbsPagination('destroy');
	$('#pagination').twbsPagination({
		currentPage: currentPagea,
	    totalPages:totalpages,
	    first: "首页",
	    last: "未页",
	    prev: '上一页',
	    next: '下一页',
	    startPage: currentPagea,
	    visiblePages:datas.rs.page.pages>10?10:datas.rs.page.pages,// 解决当totalPages小于visiblePages页码变负值的bug		    
	    initiateStartPageClick:false,
		onPageClick: function (event, page) {
			isSearch = 0;
			ajaxJson(page,datas.rs.page.pageSize);
		}
    });
}
function toTwo(_this){
	var cateId = $(_this).data("cateId");
	var cateName = $(_this).data("cateName");
	location.href="goodCateTwoList.html?cateId="+cateId+"&cateName="+encodeURI(encodeURI(cateName));
}
function initTable(datas){
	var _data=[];
	if(datas && datas.length > 0){
		currentMsg = datas;
		for(var i in datas){
			var d = datas[i];
			_data.push("<tr data-id='"+d.cateId+"'>");
			_data.push("<td>"+d.cateName+"</td>");
			_data.push("<td>"+d.cateDesc+"</td>");
			_data.push("<td>"+(new Date(d.addTime)).Format("yyyy-MM-dd hh:mm:ss")+"</td>");
			_data.push("<td>"+isEmpty(d.operateName)+"</td>");
			_data.push("</tr>");
		}
	}else{
		_data.push("<tr><td colspan='4'>暂时没有数据</td></tr>");
	}
	$("#tbodys").empty().html(_data.join(""));
	initClick();
}
//异步请求数据
function ajaxJson(pageNum,pageSize){
	$("#pageNum").val(pageNum);
	if(pageSize)$("#pageSize").val(pageSize);
	var _data=$("#form1").serializeArray();
	jsonAjax("post", "/detector/detectorInfo/listPage", _data, "json", initTables);
}
function initClick(){
	$("#tbodys tr").bind('click',function(){
		$("#tbodys tr.success").removeClass("success");
		$(this).addClass("success");
	});
}
